package edu.ncsu.csc.itrust.action;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import edu.ncsu.csc.itrust.beans.LabProcedureBean;
import edu.ncsu.csc.itrust.dao.DAOFactory;
import edu.ncsu.csc.itrust.dao.mysql.LabProcedureDAO;
import edu.ncsu.csc.itrust.enums.LabProcedureStatus;
import edu.ncsu.csc.itrust.exception.DBException;


/**
 * Handles sending lab procedure information to LT's homeview
 * @author Patrick Finegan
 *
 */
public class ViewLabTechProceduresAction {

	private LabProcedureDAO lpdao;
	private long loggedInMID;
	
	/**
	 * Set up defaults
	 * 
	 * @param factory The DAOFactory used to create the DAOs used in this action.
	 * @param loggedInMID The MID of the person retrieving personnel beans.
	 */
	public ViewLabTechProceduresAction(DAOFactory factory, long loggedInMID) {
		this.lpdao = factory.getLabProcedureDAO();
		this.loggedInMID = loggedInMID;
	}
	
	/**
	 * Returns all LabProcedures for LT with In Transit status
	 * @return List of In Transit Lab Procedures, sorted by timestamp
	 */
	public List<LabProcedureBean> getInTransit() throws DBException{
		List<LabProcedureBean> temp = lpdao.getProceduresForTech(loggedInMID,"In Transit");
		
		return temp;
	}
	
	/**
	 * Returns all LabProcedures for LT with Recieved status
	 * The first bean in the list needs to be marked as "Testing" when returned.
	 * PROBLEM: WHAT HAPPENS WHEN I VIEW STUFF TWICE?
	 * @return list of Received Lab Procedures, sorted by priority then timestamp
	 * @throws DBException
	 */
	public List<LabProcedureBean> getReceived() throws DBException{
		
		ArrayList<LabProcedureBean> list1 = new ArrayList<LabProcedureBean>();
		ArrayList<LabProcedureBean> list2 = new ArrayList<LabProcedureBean>();
		ArrayList<LabProcedureBean> list3 = new ArrayList<LabProcedureBean>();
		LabProcedureBean bean = null;
		List<LabProcedureBean> testing = null;
		
		/*
		 * get all procedures for tech with status as testing
		 * for use in setting first returned procedure with received 
		 * as testing or not if already has testing procedure
		 */
		testing = lpdao.getProceduresForTech(loggedInMID,"TESTING");
			
		
		/*
		 * sorts the list by priority
		 */
		List<LabProcedureBean> temp = lpdao.getProceduresForTech(loggedInMID,"RECEIVED");
		if(!temp.isEmpty()){
		for(int i=0;i<temp.size();i++){
			switch ((bean = temp.get(i)).getPriority()){
			case 1:
				list1.add(bean);
				break;
			case 2:
				list2.add(bean);
				break;
			case 3:
				list3.add(bean);
			}
		}
		
		//clears temp list
		temp.clear();		
		
		/*
		 * add the sorted priority lists to temp
		 * and returns
		 */
		temp.addAll(list1);
		temp.addAll(list2);
		temp.addAll(list3);
		
		/*
		 * set first procedure as testing or add the testing procedure to head of list
		 */
		if(testing.isEmpty()){
			lpdao.updateStatus(temp.get(0).getProcedureID(), "TESTING");
			temp.get(0).setStatus("TESTING");
		}else{
			temp.add(0, testing.get(0));
		}
		}
		return temp;
	}
	
	/**
	 * Reports the results for a lap procedure of id=procedureID
	 * @param procedureID -- id of the procedure
	 * @throws DBException
	 */
	public void reportResult(long procedureID, String result){
		try{
			lpdao.reportProcedureResults(procedureID, result);
		}catch(DBException e){
			e.printStackTrace();
		}
	}
	
	/**
	 * Updates an In-transit Procedure to Recieved from the InTransit menu
	 * @param procedureID
	 */
	public void updateReceived(long procedureID) throws DBException{
		lpdao.updateStatus(procedureID, LabProcedureStatus.Received.toString() );
	}
	
	
	
}
